package cn.wonderland.service.impl;

import cn.wonderland.domain.Build;
import cn.wonderland.domain.Device;
import cn.wonderland.domain.Floor;
import cn.wonderland.feign.DeviceFeignClient;
import cn.wonderland.mapper.BuildMapper;
import cn.wonderland.service.BuildService;
import cn.wonderland.util.JSONResult;
import cn.wonderland.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class BuildServiceImpl implements BuildService {

    @Autowired
    private BuildMapper buildMapper;

    @Autowired
    private DeviceFeignClient deviceFeignClient;

    /*
     * 批量新建楼层信息
     * */
    @Override
    public void addFloor(List<Floor> floors) {
        buildMapper.batchInsertFloor(floors);
    }


    /*
     * 根据楼宇id查询设备
     * getDeviceListByBuildId
     * */
    @Override
    public JSONResult<BuildVo> getDeviceListByBuildId(Integer id) {
        Build build = buildMapper.findBuildByBuildId(id);

        List<FloorListVo> floorListVos = buildMapper.findFloorByBuildId(build.getId())
                .stream().map(floor -> {
                    FloorListVo vo = new FloorListVo();
                    vo.setName(floor.getName());
                    vo.setDeviceList(deviceFeignClient.getDeviceByBuildId(build.getId())
                            .stream().map(device -> {
                                DeviceBuildVo deviceBuildVo = new DeviceBuildVo();
                                deviceBuildVo.setName(device.getName());
                                deviceBuildVo.setType(device.getType().equals("1") ? "烟感" : device.getType().equals("2") ? "手报" : device.getType().equals("3") ? "摄像头" : "");
                                return deviceBuildVo;
                            }).collect(Collectors.toList()));
                    return vo;
                }).collect(Collectors.toList());

        BuildVo buildVo = new BuildVo();
        buildVo.setBuildName(build.getBuildName());
        buildVo.setFloorList(floorListVos);

        return JSONResult.<BuildVo>builder().success(true).code("200").message("请求成功").data(buildVo).build();
    }

    /*
     * 根据设备类型  查找所有的设备
     * getDeviceListByDeviceType
     */
    @Override
    public JSONResult<List<DeviceListVo>> getDeviceListByDeviceType(Integer type) {
        // 1.查询所有符合条件的设备信息
        List<Device> devices  = deviceFeignClient.getDeviceByType(type);
        // 2.按照楼宇名称和楼层号进行分组
        Map<String, Map<String, List<Device>>> deviceGroups = devices.stream().collect(Collectors.groupingBy(
                device -> buildMapper.findBuildByBuildId(buildMapper.findFloorByFloorId(device.getFloorId()).getBuildId()).getBuildName(),
                Collectors.groupingBy(device -> buildMapper.findFloorByFloorId(device.getFloorId()).getName())));
        // 3.构造返回结果
        List<DeviceListVo> buildVos = new ArrayList<>();
        deviceGroups.forEach((buildName, floors) -> {
            List<DeviceTypeVo> deviceTypeVos = new ArrayList<>();
            floors.forEach((floorName, devicesInFloor) -> {
                List<DeviceBuildVo> dList = devicesInFloor.stream().map(device ->
                                new DeviceBuildVo(device.getName(), getDeviceTypeString(device.getType())))
                        .collect(Collectors.toList());
                DeviceTypeVo deviceTypeVo = new DeviceTypeVo();
                deviceTypeVo.setName(floorName);
                deviceTypeVo.setDevice(dList);
                deviceTypeVos.add(deviceTypeVo);
            });
            DeviceListVo buildVo = new DeviceListVo();
            buildVo.setBuildName(buildName);
            buildVo.setFloorList(deviceTypeVos);
            buildVos.add(buildVo);
        });
        // 4.构造返回 JSON 结果对象
        return JSONResult.<List<DeviceListVo>>builder().success(true).message("请求成功").code("200").data(buildVos).build();
    }

    private String getDeviceTypeString(Integer type) {
        switch (type) {
            case 1:return "烟感";
            case 2:return "手报";
            case 3:return "摄像头";
            default:return "";
        }
    }
}
